package com.smsc.headend.task.engine.handler.impl.deviceconfig;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import com.smsc.headend.common.enums.errorcode.TaskHandlerErrorCode;
import com.smsc.headend.common.utils.RedisUtils;
import com.smsc.headend.module.data.enums.OperationGroup;
import com.smsc.headend.module.data.enums.OperationName;
import com.smsc.headend.module.protocol.DeviceData;
import com.smsc.headend.module.task.dto.AtomicTask;
import com.smsc.headend.module.task.dto.Task;
import com.smsc.headend.task.engine.dao.TaskDAO;
import com.smsc.headend.task.engine.handler.TaskHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;

import java.util.Map;

/**
 * (业务实现)
 *
 * @author wjh
 * @date 2022/4/13 14:25.
 */
@Slf4j
public class SetStepTariffHandler implements TaskHandler {
    @Autowired
    TaskDAO taskDAO;
    @Autowired
    KafkaTemplate kafkaTemplate;
    @Autowired
    RedisUtils redisUtils;

    @Override
    public int handle(Task task, AtomicTask atomicTask, DeviceData cosemData, String actionResult) {
        log.info("{} action result {}", atomicTask.getAtomicTaskNo(), actionResult);
        Map<String, Object> param = getParameterMap(task, atomicTask);
        if (param.get("dataChannel") != null && param.get("dataChannel").equals("true")) {
            // 数据通道报文下发 可能有get set action, 执行成功的话都返回sent success,不做解析处理
            actionResult = TaskHandlerErrorCode.PACKET_SENT_SUCCESS.getErrorText();
        }
        String stDataGroup = Convert.toStr(param.get("stDataGroup"));
        String seq = Convert.toStr(param.get("seq"));
        Map<String, String> result = MapUtil.newHashMap();
        result.put("stDataGroup", stDataGroup);
        result.put("seq", seq);
        result.put("value", actionResult);
        OperationName operationName = OperationName.forId(atomicTask.getOperationId());
        taskDAO.addTaskData(task.getTaskNo(), atomicTask.getAtomicTaskNo(), operationName.getName(), result, 600L, true);
        if (isTaskLastAtomicTask(task, atomicTask)) {
            taskDAO.addTaskData(task.getTaskNo(), atomicTask.getAtomicTaskNo(), "subTaskCompleted", true, 600L, true);
        }
        return 1;
    }

    @Override
    public int handleFailure(Task task, AtomicTask atomicTask, String message) {
        log.info("error {}, ", message);
        if (atomicTask.getGroupId() == OperationGroup.StepTariffActiveDateSet.getId().intValue()
                || atomicTask.getGroupId() == OperationGroup.StepTariffBackUpRatePriceSet.getId().intValue()
                || atomicTask.getGroupId() == OperationGroup.StepTariffBackUpStepSet.getId().intValue()
                || atomicTask.getGroupId() == OperationGroup.StPaymentControlConfigSet.getId().intValue()
                || atomicTask.getGroupId() == OperationGroup.StFunctionControlConfigSet.getId().intValue()) {
            String operationGroupName = OperationGroup.forId(atomicTask.getGroupId().intValue()).getName();
            String stepOperationName = OperationName.forId(atomicTask.getOperationId()).getName();
            if (task.getIsODR()) {
                taskDAO.addTaskData(task.getTaskNo(), atomicTask.getAtomicTaskNo(), stepOperationName, String.format("step %s failed, [%s]", operationGroupName, message), 3600l, false);
            }
        }
        return -1;
    }
}
